<?php

namespace Library\Prizes;

use Library\CardCode;
use Library\Task\DbTask;
use Library\User;
use Think\Log;

/**
 * @author pufan
 */
class Entity extends Prizes{

    public function __construct($desc) {
        parent::__construct($desc);
        $this->desc['type'] = Prizes::Entity;
        if ($this->get_real_card_code() == TRUE) {
            $this->init_order();
        } else {
            $this->valid = FALSE; //奖品设置为无效,
        }
    }
    
    /*
     *  获取卡券号码
     */
    private function get_real_card_code() {
        $card_code = $this->cardCode->get_card($this->get_order_attr('card_id'));
        
        if ($card_code) {
            $this->set_order_attr('card_num', $card_code['card_number']);
            $this->set_order_attr('card_list_id', $card_code['id']);
            return TRUE;
        } else {
            \Think\Log::record("get_real_card_code card_code empty"); 
        }
        
        return FALSE;
    }
    
    protected function save_order() {
        $data['card_type'] = 5;
        $data['event_id'] = $this->get_order_attr('event_id');
        $data['card_id'] = $this->get_order_attr('card_id');
        $data['openid'] = $this->get_order_attr('openid');
        $data['userid'] = $_SESSION['user_id'];
        $data['order_status'] = 0;
        $data['card_num'] = $this->get_order_attr('card_num');  //卡券号
        $data['winning_time'] = $this->winning_time;  //
        $data['sn_number'] = $this->sn_number;  //
        
        $this->prizes_order_model->startTrans(); 
        try {
            $result = $this->prizes_order_model->add($data);

            if ($result) { //统计中奖次数
                $result = $this->update_user_prizes_count($this->get_order_attr('card_id'), $this->get_order_attr('openid'));
            }
                  
            if (!$result){ 
                \Think\Log::record('order write mysql fail sql:', $this->prizes_order_model->getLastSql());
                \Think\Log::record('order write mysql fail arg:', json_encode($data));
            }
            $this->add_recovery_cardcode_task();
        } catch (\Exception $e) {
           $result = FALSE;
           \Think\Log::record('save_order catch Exception'.$e->getMessage());
        }

        if ($result){
            // 提交事务
            $this->prizes_order_model->commit(); 
        }else{
           // 事务回滚
           $this->prizes_order_model->rollback(); 
           $this->cardCode->free_card($this->get_order_attr('card_id'), $this->get_order_attr('card_list_id'));
           
           $this->valid = FALSE; //奖品设置为无效,
           \Think\Log::record("prizes order fail");
        }
    }

    /*
     * 增加卡券回收
     */
    protected function add_recovery_cardcode_task() {
        $taskid = 'recoveryCardCode_'.$this->get_order_attr('card_id').'_'.$this->get_order_attr('card_num').'_'.mt_rand(1000, 9999);  //卡券号
        $prizesTimeout = C('PRIZES_TIMEOUT') ? C('PRIZES_TIMEOUT') : 60;
        $eventTime = date('Y-m-d H:i:s', strtotime ($this->winning_time) + $prizesTimeout); //当前时间  ,注意H 是24小时 h是12小时

        $data = ' CALL `recovery_cardcode`('.$this->get_order_attr('card_id').', "'.$this->get_order_attr('card_num').'"); ';

        $sche = new DbTask();
        $sche->add($taskid, $data, $eventTime);
    }
    
    public function get_view_desc() {
        $data['title'] = $this->get_order_attr('title'); 
        $data['center_pic'] = $this->get_order_attr('center_pic'); 
        $data['card_code'] = $this->get_order_attr('card_num');
        $data['description'] = $this->get_order_attr('description');
        $data['prizes_grade'] = $this->get_order_attr('prizes_grade');
        return $data;
    }
}
